package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.Collect;
import chuishop.yiqimaicai.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CollectController {

    @Autowired
    private CollectService collectService;

    @RequestMapping("selectCollectByUid")
    @ResponseBody
    public List<Collect> selectCollectByUid(Integer uid){
        List<Collect> list = collectService.selectCollectByUid(uid);
        return list;
    }

    //添加收藏
    @RequestMapping("addCollect")
    @ResponseBody
    public Map<Object, Object> addCollect(@RequestBody Collect collect){

        Map<Object, Object> result = new HashMap<>();

        boolean flag = collectService.addCollect(collect);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    //删除收藏
    @RequestMapping("delCollect")
    @ResponseBody
    public Map<Object, Object> delCollect(String ids){

        String idstr[] = ids.split(",");

        Map<Object, Object> result = new HashMap<>();

        boolean flag = collectService.delCollect(idstr);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

}
